//
//  DDEmojiCollectionView.swift
//  swift微博
//
//  Created by whitcast on 16/7/21.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit

let DDEmojiCollectionViewCellIndentifier = "DDEmojiCollectionViewCellIndentifier"

class DDEmojiCollectionView: UICollectionView {
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.minimumLineSpacing = 0
        
        flowLayout.minimumInteritemSpacing = 0
        
        flowLayout.scrollDirection = .Horizontal
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
     //设置UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI(){
        
       //数据源
        dataSource = self
        //注册cell
        self.registerClass(DDEmojiCollectionCell.self, forCellWithReuseIdentifier: DDEmojiCollectionViewCellIndentifier)
        //分页效果
        self.pagingEnabled = true

    }
    // MARK:---获取数据源
    //Collection的数据源
    lazy var infosArr: [[[DDEmojiModel]]] = {
        
        var marr = [[[DDEmojiModel]]]()
        
        marr.append(self.defaultData)
        marr.append(self.emojiData)
        marr.append(self.lxhData)
        
        return marr
        
    }()
    //对应cell个数的数组,一个cell使用一个数组
    lazy var defaultData: [[DDEmojiModel]] = {
        let arr = self.getCellNumber(self.defaultDataCell)
        return arr
    }()
    
    lazy var emojiData: [[DDEmojiModel]] = {
        let arr = self.getCellNumber(self.emojiDataCell)
        return arr
    }()
    lazy var lxhData: [[DDEmojiModel]] = {
        let arr = self.getCellNumber(self.lxhDataCell)
        return arr
    }()
    //默认的二维数组  -- 对应默认表情的cell个数
    private func getCellNumber(arr: [DDEmojiModel])->[[DDEmojiModel]] {//count 是一级数组的原数个数
        
        let pages = (arr.count - 1)/20 + 1
        //拆分数组
        var marr = [[DDEmojiModel]]()
        for i in 0..<pages{
            
            let loc = 20 * i
            var len = 20
            if loc + len > arr.count {
                len = arr.count - loc
            }
            
            let subArray = (arr as? NSArray)?.subarrayWithRange(NSRange.init(location: loc, length: len)) as? [DDEmojiModel]
            
            marr.append(subArray ?? [])
        }
        return marr
    }
    
    //emoji一级数组  默认表情的总数据
    lazy var defaultDataCell:[DDEmojiModel] = {
        
        let arr = self.getData("default/info.plist")
        
        return arr
    }()
    //浪小花 一级数组  浪小花表情的总数据
    lazy var lxhDataCell: [DDEmojiModel] = {
        let arr = self.getData("lxh/info.plist")
        return arr
    }()
    
    //emoji表情 一级数组 emoji的总数据
    lazy var emojiDataCell: [DDEmojiModel] = {
        let arr = self.getData("emoji/info.plist")
        return arr
    }()
    
    //路径bundle  获取文件的上一级目录 bundle
    private let emojiBundle: NSBundle = {
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        let bundle = NSBundle(path: path)!
        
        print(bundle)
        
        return bundle
    }()
    //获取默认的数据
    private func getData(subPath: String)->[DDEmojiModel]{
        
        let bundle = self.emojiBundle
        
        let path1 = bundle.pathForResource(subPath, ofType: nil)//此方法 可以省略中间的content和resourse路径
        
        let arr = NSArray(contentsOfFile: path1 ?? "") ?? []
        
        var marr = [DDEmojiModel]()
        
        for value in arr {
            let model = DDEmojiModel.init(dict: value as! [String : AnyObject])
            
            marr.append(model)
        }
        
        return marr
    }
    // MARK: --------------------------------
    
    // MARK:--设置itemsize
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = self.bounds.size
        
    }


}


//MARK: -- 数据源方法

extension DDEmojiCollectionView: UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{
        return infosArr.count
    }

    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        
        return infosArr[section].count
    }
    


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(DDEmojiCollectionViewCellIndentifier, forIndexPath: indexPath) as! DDEmojiCollectionCell
        
        return cell
    }

}

